package com.lz.football.module.guess
{
	import com.common.stdlib.ArrayUtil;
	import com.common.utils.DisplayObjectUtil;
	import com.common.utils.Reflection;
	import com.global.GlobalDef;
	import com.liangzhong.events.GuessEvent;
	import com.liangzhong.events.ModuleEvent;
	import com.liangzhong.model.AppData;
	import com.liangzhong.model.footballLottery.FootballLotteryDataManager;
	
	import common.comp.TabButtonBar;
	
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import lz.controls.LButton;
	import lz.controls.LItem;
	import lz.controls.PageComponent;
	import lz.controls.core.TitleWindow;
	import lz.controls.events.PageEvent;
	import lz.controls.managers.PopUpManager;

	/**
	 *竞猜 
	 * @author lake
	 * 
	 */	
	public class GuessView extends TitleWindow implements IGuessView
	{
		/**
		 *规则按钮 
		 */		
		private var ruleBtn			:SimpleButton;
		/**
		 *竞猜主选项视图 
		 */		
		private var mainTabButtonBar 	:TabButtonBar;
		/**
		 *内容选项视图 
		 */		
		private var contTabButtonBar	:TabButtonBar;
		/**
		 *列表容器 
		 */		
		private var itemListContainer 	:Sprite;
		/**
		 * 列表条目
		 */		
		private var itemList			:Array = [];
		/**
		 *系统列表条目 
		 */		
		private var sysItemList		:Array = [];
		/**
		 *我的竞猜列表条目 
		 */		
		private var myItemList			:Array = [];
		/**
		 *内容容器 
		 */		
		private var contContainer		:Sprite;
		/**
		 *押注结果面板 
		 */		
		private var betResultPanel		:BetResultPanel;
		/**
		 *分页容器 
		 */		
		private var pageContainer		:Sprite;
		/**
		 *每页显示条数 
		 */		
		private var pageSize 			:int = 4;
		/**
		 *分页 
		 */		
		public var pageComp 			:PageComponent;
		/**
		 *规则面板 
		 */		
		private var guessRulePanel		:GuessRulePanel;
		/**
		 *主选项状态 
		 * 0 竞猜题目
		 * 1 我的竞猜 
		 */		
		private var mainStatus			:int;
		/**
		 *data 
		 */		
		private var _data			:*;
		
		public function GuessView()
		{
			super();
		}
		/**
		 *获取设置数据 
		 * @param value
		 * 
		 */		
		public function set data(value:*):void
		{
			_data = value;
			//
			var fbList :Array = _data;
			var listLen :int = fbList.length;
			if(mainStatus == 0)//竞猜题目
			{
				pageSize = 5;
			}
			else
			{
				pageSize = 4;
			}
			///page 
			if(listLen > pageSize)
			{
				pageContainer.visible = true;
			}
			else
			{
				pageContainer.visible = false;
			}
			pageComp.pageSize = pageSize;
			pageComp.totalNumbers = listLen;
			pageComp.curPage = 1;
			contContainer.addChild(itemListContainer);
		}
		public function get data():*
		{
			return _data;
		}
		/**
		 *初始化 
		 * 
		 */
		public function initUI(view:Object=null):void
		{
			_symbolSkin = Reflection.createInstance("com.lz.football.guess.viewSkin");
			_symbolSkin.cacheAsBitmap = true;
			//初始化宽高
			width = _symbolSkin.width;
			height = _symbolSkin.height;
			//初始化UI界面控件
			windowBg = _symbolSkin.windowBg;//背景框
			windowBg.mouseChildren = false;
			ruleBtn = _symbolSkin.ruleBtn;
			//设置可拖动
			dragEnabled = true;
			this.addChild(_symbolSkin);
			closeBtn = Reflection.createInstance("com.lz.commonUI.closeBtn");
			this.addChild(closeBtn);
			closeBtn.x = 590;
			closeBtn.y = 38;
			//设置关闭按钮
			setCloseBtn(closeBtn);
			//选项bar 初始化
			mainTabButtonBar = new TabButtonBar();
			contTabButtonBar = new TabButtonBar();
			var i :int = 1;
			var lbtn :LButton;
			var lbtns :Array = [];
			for(;i<=2;i++)
			{
				lbtn = new LButton(_symbolSkin["buttonBar"]["skin_" + i]);
				lbtn.enable = true;
				lbtns.push(lbtn);
			}
			mainTabButtonBar.setButtons(lbtns);
			mainTabButtonBar.selectTabButton(lbtns[0]);
			i = 1
			lbtns = [];
			for(;i<=4;i++)
			{
				lbtn = new LButton(_symbolSkin["buttonBar1"]["skin_" + i]);
				lbtn.enable = true;
				lbtns.push(lbtn);
			}
			contTabButtonBar.setButtons(lbtns);
			contTabButtonBar.selectTabButton(lbtns[0]);
			
			contContainer = new Sprite();
			_symbolSkin.addChild(contContainer);
			contContainer.x = 15;
			contContainer.y = 141;
			var testItem :FbLotteryItem;
			for(i = 0;i<5;i ++)
			{
				testItem = new FbLotteryItem();
				testItem.initUI();
				testItem.y = i * 62;
				sysItemList.push(testItem);
			}
			var testItem1 :MyGuessItem;
			for(i = 0;i<4;i ++)
			{
				testItem1 = new MyGuessItem();
				testItem1.initUI();
				testItem1.y = i * 72;
				myItemList.push(testItem1);
			}
			itemListContainer = new Sprite();
			betResultPanel = new BetResultPanel();
			betResultPanel.initUI();
			pageContainer = new Sprite();
			pageContainer.x = 280;
			pageContainer.y = 454;
			_symbolSkin.addChild(pageContainer);
			var pageSkin :MovieClip = Reflection.createInstance("com.liangzhong.ui.pageComp");
			pageContainer.addChild(pageSkin);
			pageComp = new PageComponent();
			pageComp.initUI(pageSkin);
			pageComp.nextBtn.x += 3.9;
			pageContainer.visible = false;
			guessRulePanel = new GuessRulePanel();
			guessRulePanel.initUI();
			//add event
			addEvtListener();
			//set data
			data = FootballLotteryDataManager.instance.fbLotteryList;
		}
		/**
		 *注册事件 
		 * 
		 */	
		public function addEvtListener():void
		{
			pageComp.addEventListener(PageEvent.PAGE_CHANGE,onPageChange);
			mainTabButtonBar.addEventListener(Event.CHANGE,mainTabUpdateHandler);
			contTabButtonBar.addEventListener(Event.CHANGE,contTabUpdateHandler);
			ruleBtn.addEventListener(MouseEvent.CLICK,onRuleHandler);
			contContainer.addEventListener("betResultDetail",betResultDetailHandler);
			contContainer.addEventListener("betResultPReturn",betResultPanelReturnHandler);
			FootballLotteryDataManager.instance.addEventListener(Event.CHANGE + "_list",listHandler);
			FootballLotteryDataManager.instance.addEventListener(Event.CHANGE + "_my",myHandler);
		}
		/**
		 *主选项卡改变 
		 * @param e
		 * 
		 */		
		private function mainTabUpdateHandler(e :Event):void
		{
			mainStatus = mainTabButtonBar.selectType;
			contTabButtonBar.setTabButtonByIndex(0);
		}
		/**
		 *内容选项卡改变 
		 * @param e
		 * 
		 */		
		private function contTabUpdateHandler(e :Event):void
		{
			showDatas();
		}
		/**
		 *显示数据 
		 */		
		private function showDatas():void
		{
			if(betResultPanel && betResultPanel.parent)
			{
				betResultPanel.parent.removeChild(betResultPanel);
			}
			var sts :int = contTabButtonBar.selectType;
			if(mainStatus == 0)//竞猜题目
			{
				if(sts == 0)
				{
					data = FootballLotteryDataManager.instance.fbLotteryList;
				}
				else
				{
					data = FootballLotteryDataManager.instance.getGuessListByStatus(sts);
				}
			}
			else//我的竞猜
			{
				if(sts == 0)
				{
					data = FootballLotteryDataManager.instance.myGuessList;
				}
				else
				{
					data = FootballLotteryDataManager.instance.getMyGuessListByStatus(sts);
				}
			}
		}
		/**
		 *押注 结果 查看  详情
		 * @param e
		 * 
		 */		
		private function betResultDetailHandler(e :Event):void
		{
			DisplayObjectUtil.removeAllChildren(contContainer);
			contContainer.addChild(betResultPanel);
			betResultPanel.data = e.target.data;
			if(pageComp.maxPages > 1)
			{
				pageContainer.visible = false;
			}
		}
		/**
		 *押注面板返回 
		 * @param e
		 * 
		 */		
		private function betResultPanelReturnHandler(e :Event):void
		{
			DisplayObjectUtil.removeAllChildren(contContainer);
			showDatas();
		}
		/**
		 *规则 
		 * @param e
		 * 
		 */		
		private function onRuleHandler(e :MouseEvent):void
		{
			PopUpManager.addPopUp(guessRulePanel,GlobalDef.AlertContainer,GlobalDef.StageWidth,GlobalDef.StageHeight,true);
		}
		/**
		 * onCloseHandler
		 * @param e
		 * 
		 */		
		override protected function onCloseHandler(e :MouseEvent):void
		{
			this.dispatchEvent(new ModuleEvent(ModuleEvent.REMOVE_MODULE));
		}
		/**
		 *竞猜列表 
		 * @param e
		 * 
		 */		
		private function listHandler(e :Event):void
		{
			if(betResultPanel.parent)
			{
				return;
			}
			showDatas();
		}
		/**
		 *我的竞猜列表 
		 * @param e
		 * 
		 */		
		private function myHandler(e :Event):void
		{
			if(betResultPanel.parent)
			{
				return;
			}
			showDatas();
		}
		/**
		 *删除事件 
		 * 
		 */	
		public function removeEvtListener():void
		{
			closeBtn.removeEventListener(MouseEvent.CLICK,onCloseHandler);
			pageComp.removeEventListener(PageEvent.PAGE_CHANGE,onPageChange);
			mainTabButtonBar.removeEventListener(Event.CHANGE,mainTabUpdateHandler);
			contTabButtonBar.removeEventListener(Event.CHANGE,contTabUpdateHandler);
			ruleBtn.removeEventListener(MouseEvent.CLICK,onRuleHandler);
			contContainer.removeEventListener("betResultDetail",betResultDetailHandler);
			contContainer.removeEventListener("betResultPReturn",betResultPanelReturnHandler);
			FootballLotteryDataManager.instance.removeEventListener(Event.CHANGE + "_list",listHandler);
			FootballLotteryDataManager.instance.removeEventListener(Event.CHANGE + "_my",myHandler);
		}
		/**
		 *page change 
		 * @param e
		 * 
		 */		
		private function onPageChange(e :Event):void
		{
			setGuessData(ArrayUtil.getPageSizeDatas(pageComp.curPage,pageComp.pageSize,_data));
		}
		private function setGuessData(listDatas:Array):void
		{
			DisplayObjectUtil.removeAllChildren(itemListContainer);
			var len :int = listDatas.length;
			var item :LItem;
			if(mainStatus == 0)//竞猜题目
			{
				itemList = sysItemList;
			}
			else
			{
				itemList = myItemList;
			}
			for(var i :int = 0;i<len;i++)
			{
				item = itemList[i];
				item.data = listDatas[i];
				itemListContainer.addChild(item);
			}
		}
		/**
		 *销毁 
		 * 
		 */	
		public function destroyView():void
		{
			removeEvtListener();
			if(this.parent)
			{
				this.parent.removeChild(this);
			}
			DisplayObjectUtil.removeAllChildren(this);
			DisplayObjectUtil.removeAllChildren(itemListContainer);
			DisplayObjectUtil.removeAllChildren(contContainer);
			DisplayObjectUtil.removeAllChildren(pageContainer);
			_symbolSkin = null;
			closeBtn = null;
			for each(var sysItem :FbLotteryItem in sysItemList)
			{
				sysItem.destroyView();
				sysItem = null;
			}
			sysItemList = null;
			for each(var myItem :MyGuessItem in myItemList)
			{
				myItem.destroyView();
				myItem = null;
			}
			myItemList = null;
			itemList = null;
			betResultPanel.destroyView();
			betResultPanel = null;
			pageComp = null;
			mainTabButtonBar.destroyView();
			mainTabButtonBar = null;
			contTabButtonBar.destroyView();
			contTabButtonBar = null;
			guessRulePanel.destroyView();
			guessRulePanel = null;
			_data = null;
		}
		/**
		 *显示tab类型 
		 * @param $obj
		 * 
		 */
		public function showType($obj:Object):void
		{
		}
	}
}